package com.ruoyi.web.controller.minglvmini;

import com.github.pagehelper.Page;
import com.minglv.mini.domain.User;
import com.minglv.mini.service.IIntegralLogService;
import com.minglv.mini.service.IUserService;
import com.minglv.mini.tools.JWTUtils;
import com.minglv.mini.tools.MiniResult;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "API-我的2")
@RestController
@RequestMapping("/api")
public class ApiIntegralController {

    @Resource
    IIntegralLogService iIntegralLogService;

    @Resource
    IUserService userService;


    @GetMapping("/integral/list")
    @ApiOperation("我的水滴获取记录")
    public Object getList(HttpServletRequest request, @RequestParam(name = "page") Integer page,
                                @RequestParam(name = "type") Integer type){
        String token = request.getHeader("token");
        if (!checkToken(token)){
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
        Page<Object> objects = PageUtils.startPage(page, 10);
        List<Map> list = iIntegralLogService.getList(type);
        Map data = new HashMap();
        data.put("list",list);
        data.put("count",objects.getTotal());
        return MiniResult.success(data);
    }

    @GetMapping("/cheese/list")
    @ApiOperation("我的奶酪获取记录")
    public Object getCheeseList(HttpServletRequest request, @RequestParam(name = "page") Integer page,
                                @RequestParam(name = "type") Integer type){
        String token = request.getHeader("token");
        if (!checkToken(token)){
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
        Page<Object> objects = PageUtils.startPage(page, 10);
        List<Map> list = iIntegralLogService.getCheeseList(type);
        Map data = new HashMap();
        data.put("list",list);
        data.put("count",objects.getTotal());
        return MiniResult.success(data);
    }


    /**
     * 校验token
     * @param token
     * @return
     */
    Boolean checkToken(String token){
        if (StringUtils.isEmpty(token)) {
            return false;
        }
        String[] s = token.split(" ");
        if (ObjectUtils.isEmpty(s) || s.length == 0) {
            return false;
        }

        token = s[1];
        Long userId = JWTUtils.getUserid(token);
        if (ObjectUtils.isEmpty(userId)) {
            return false;
        }

        User user = userService.selectUserById(userId);
        if (ObjectUtils.isEmpty(user)) {
            return false;
        }
        return true;
    }
}
